﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using System.Windows.Browser;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace Microsoft.ProductivityHub.SilverlightBookshelf
{
	public partial class MyFavoritesControl : UserControl
	{
        SharePointData sd = new SharePointData();
        List<ContentItem> _contentItems = null;

        public void LoadData()
        {
            sd.QueryFavorites();
        }


        void sd_DataLoadedEvent(List<ContentItem> contentItems)
        {
            _contentItems = contentItems;
            favoritesContentControl.ItemsSource = _contentItems;
        }
        

        public MyFavoritesControl()
		{
			// Required to initialize variables
			InitializeComponent();
            sd.DataLoadedEvent += new SharePointData.DataLoaded(sd_DataLoadedEvent);

            // do not perform LoadData until the tab is requested (Bookshelf.cs), otherwise you may get a race condition with the loading of the JS
            // LoadData();
            

		}

        private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            double offset = scrollView.VerticalOffset - (e.Delta / 3);
            if (offset < 0)
            {
                scrollView.ScrollToVerticalOffset(0);
            }
            else if (offset > scrollView.ExtentHeight)
            {
                scrollView.ScrollToVerticalOffset(scrollView.ExtentHeight);
            }
            else
            {
                scrollView.ScrollToVerticalOffset(offset);
            }
            e.Handled = true;

        }


        private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string docUrl = ((TextBlock)sender).Tag as string;
            if (!String.IsNullOrEmpty(docUrl))
            {
                string currentSiteUrl = ApplicationContext.Current.Url;
                if (!String.IsNullOrEmpty(currentSiteUrl))
                {
                    HtmlPage.Window.Navigate(new Uri(docUrl));
                }
            }

        }

        private void RemoveFavorite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string docUrl = ((TextBlock)sender).Tag as string;
            string currentSiteUrl = ApplicationContext.Current.Url;
            if (!String.IsNullOrEmpty(currentSiteUrl))
            {
                // we have sharepoint context, attempt the script call
                // javascript code to call: javascript:hub10_RemoveFavorite(encodedUrl)) 
                string scriptCall = String.Format("hub10_RemoveFavorite('{0}');", docUrl);
                HtmlPage.Window.Eval(scriptCall);

                LoadData();

            }

        }


	}
}